<template>
    <div>
        <!--  -->
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 医生管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>医生信息</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <!--  -->
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="请输入医疗人员姓名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="doctorId" label="编号" width="50" align="center"></el-table-column>
                <el-table-column prop="doctorName" label="姓名"></el-table-column>
                <el-table-column prop="doctorGender" label="性别"></el-table-column>
                <el-table-column prop="doctorAge" label="年龄"></el-table-column>
                <el-table-column prop="doctorSectionOffice" label="科室"></el-table-column>
                <el-table-column prop="doctorPhone" label="手机"></el-table-column>
                <el-table-column prop="doctorIntro" label="简介" show-overflow-tooltip=true width="150"></el-table-column>
                <el-table-column prop="createTime" label="添加时间"></el-table-column>
                <el-table-column prop="updateTime" label="修改时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">修改
                        </el-button>
                        <el-button type="text" icon="el-icon-delete" class="red"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.current"
                    :page-size="query.pageSize" :total="query.total" @current-change="handlePageChange">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="修改" v-model="editVisible" width="30%">
            <el-form label-width="70px">
                <el-form-item label="编号">
                    <el-input v-model="form.doctorId" disabled></el-input>
                </el-form-item>
                <el-form-item label="姓名">
                    <el-input v-model="form.doctorName"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="doctorGender">
                    <el-radio-group v-model="form.doctorGender">
                        <el-radio label="男" name="男"></el-radio>
                        <el-radio label="女" name="女"></el-radio>
                        <el-radio label="其他" name="其他"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="年龄">
                    <el-input v-model="form.doctorAge"></el-input>
                </el-form-item>
                <el-form-item label="科室">
                    <el-input v-model="form.doctorSectionOffice"></el-input>
                </el-form-item>
                <el-form-item label="手机">
                    <el-input v-model="form.doctorPhone"></el-input>
                </el-form-item>
                <el-form-item label="简介">
                    <el-input v-model="form.doctorIntro"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { onActivated, ref, reactive, inject } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getDoctorList, modifyDoctor, deleteDoctor } from "../../api";

export default {
    name: "doctor",
    setup() {
        const query = reactive({
            current: 1,
            name: '',
            pageSize: 10,
            total: 0,
        });
        const tableData = ref([]);
        const pageTotal = ref(0);
        const reload  = inject("reload");
        // let tableData = [];
        // 获取表格数据
        const getData = () => {
            getDoctorList(query).then((res) => {
                console.log(res);
                tableData.value = res.data.message;
                query.total = res.data.total;
            });
        };
        getData();

        // 查询操作
        const handleSearch = () => {
            query.current = 1;
            getData();
        };
        // 分页导航
        const handlePageChange = (val) => {
            query.current = val;
            getData();
        };

        // 删除操作
        const handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {
                type: "warning",
            }).then(() => {
                deleteDoctor(row.doctorId).then(res => {
                    if (res.code === 200) {
                        reload();
                        ElMessage.success('删除成功');
                    } else {
                        console.log("[删除人员] 失败: " + res.message);
                        ElMessage.error('服务器繁忙，请稍后重试');
                    }
                })
            }).catch(() => {});
        };

        // 表格编辑时弹窗和保存
        const editVisible = ref(false);
        let form = reactive({
            doctorId: null,
            doctorName: '',
            doctorGender: '',
            doctorAge: null,
            doctorPhone: '',
            doctorSectionOffice: '',
            doctorIntro: '',
        });
        const handleEdit = (index, row) => {
            Object.keys(form).forEach((item) => {
                form[item] = row[item];
            });
            editVisible.value = true;
        };
        const saveEdit = () => {
            modifyDoctor(form).then(res => {
                if (res.code === 200) {
                    reload();
                    ElMessage.success('修改成功');
                } else {
                    console.log("[修改人员] 失败: " + res.message);
                    ElMessage.error('服务器繁忙，请稍后重试');
                }
            });
            editVisible.value = false;
        };

        onActivated(() => {
            console.log("Doctor onActivated!");
            getData();
        });

        return {
            query,
            tableData,
            pageTotal,
            editVisible,
            form,
            handleSearch,
            handlePageChange,
            handleDelete,
            handleEdit,
            saveEdit,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
</style>
